package com.richapp.launcher.scene.conditions.sensor;

import android.util.Log;

import com.richapp.launcher.database.system.dao.SceneModelDao;
import com.richapp.launcher.database.system.mode.DeviceModel;
import com.richapp.launcher.database.system.mode.SceneModel;
import com.richapp.launcher.log.LoggerServer;
import com.richapp.launcher.scene.SceneServer;
import com.richapp.launcher.scene.abstracts.SceneAbstractSensorConditions;
import com.richapp.launcher.scene.interfaces.ISceneConditions;
import com.richapp.launcher.scene.interfaces.ISceneSensorConditions;

public class SceneSensorAirConditions extends SceneAbstractSensorConditions {

	private String checkAirValue;

	public SceneSensorAirConditions() {
		
	}

	@Override
	public int getConditionType() {
		return ISceneConditions.SCENE_CONDITION_TYPE_SENSOR;
	}

	@Override
	public int getConditionSubType() {
		return ISceneSensorConditions.SCENE_CONDITION_SUB_TYPE_SENSOR_AIR;
	}

	@Override
	public boolean checkCondition(String sceneName) {
		boolean checkFlag=false;
		SceneModel sceneModel = new SceneModel();
		SceneModelDao sceneModelDao = new SceneModelDao(SceneServer.getInstance().getContext());
		sceneModel=sceneModelDao.getSceneModelOnly(sceneName);
		Log.d(getClass().getName(),"检测 空气质量条件");
		DeviceModel device = getDevice();
		Log.d(getClass().getName(),"获取到的设备:"+device);
		if(device != null){
			Log.d(getClass().getName(),"设备："+ device.getDeviceName() + " 空气质量:"+device.getAriQuality() + " 检测值："+checkAirValue);
			LoggerServer.getInstance().addLog("DEBUG",getClass().getName(),"设备："+ device.getDeviceName() + " 空气质量:"+device.getAriQuality() + " 检测值："+checkAirValue);
			switch (getConditionCheckModel()[0]) {
				case ISceneSensorConditions.SCENE_CONDITION_VALUE_CHECK_TYPE_GREATERTHAN:
					if (device.getAriQuality() > Integer.parseInt(checkAirValue) && "true".equals(sceneModel.getStatusCycleFlag())) {
						checkFlag= true;
					}else if (device.getAriQuality()  <= Integer.parseInt(checkAirValue)){
						sceneModel.setStatusCycleFlag("true");
						sceneModelDao.add(sceneModel);
					}
					break;
				case ISceneSensorConditions.SCENE_CONDITION_VALUE_CHECK_TYPE_EQUALS:
					if (device.getAriQuality() == Integer.parseInt(checkAirValue) && "true".equals(sceneModel.getStatusCycleFlag())) {
						checkFlag= true;
					}else  if (device.getAriQuality() != Integer.parseInt(checkAirValue)){
						sceneModel.setStatusCycleFlag("true");
						sceneModelDao.add(sceneModel);
					}
					break;

				case ISceneSensorConditions.SCENE_CONDITION_VALUE_CHECK_TYPE_LESSTHAN:
					if (device.getAriQuality() < Integer.parseInt(checkAirValue)  && "true".equals(sceneModel.getStatusCycleFlag())) {
						checkFlag= true;
					}else if (device.getAriQuality() >= Integer.parseInt(checkAirValue)){
						sceneModel.setStatusCycleFlag("true");
						sceneModelDao.add(sceneModel);
					}
					break;

				default:
					break;
			}

		}
		return checkFlag;
	}

	public String getCheckAirValue() {
		return checkAirValue;
	}

	public void setCheckAirValue(String checkAirValue) {
		this.checkAirValue = checkAirValue;
	}

}
